package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.adapter.PriceListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ProductLedgerOriginal;
import com.isunland.managesystem.entity.ProductLedgerOriginal.ProductLedgerContent;
import java.util.ArrayList;
import java.util.HashMap;

public class PriceDetailFragment extends BaseListFragment {
  public static final String EXTRA_CONTENT = "com.isunland.managesystem.entity.extra_content";
  private ProductLedgerContent mContent;
  private ArrayList<ProductLedgerContent> mlist = new ArrayList<>();

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar;
    if (activity != null) {
      actionBar = activity.getSupportActionBar();
      if (actionBar != null) {
        if (NavUtils.getParentActivityName(activity) != null) {
          actionBar.setDisplayHomeAsUpEnabled(true);
        }
      }
    }
    // 获取传过来的产品信息
    mContent = (ProductLedgerContent) getArguments().getSerializable(EXTRA_CONTENT);
    mActivity = (BaseVolleyActivity) getActivity();
    super.onCreate(savedInstanceState);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        return super.onOptionsItemSelected(item);
    }
    return super.onOptionsItemSelected(item);
  }

  public static PriceDetailFragment newInstance(ProductLedgerContent item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, item);
    PriceDetailFragment fragment = new PriceDetailFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public String getUrl() {
    return ApiConst.URL_PRICE_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("productId", mContent.getId());
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    ProductLedgerOriginal original = new Gson().fromJson(arg0, ProductLedgerOriginal.class);
    ArrayList<ProductLedgerContent> rows = (ArrayList<ProductLedgerContent>) original.getRows();
    mlist.clear();
    mlist.addAll(rows);
    setAdapter(mlist);
  }

  private void setAdapter(ArrayList<ProductLedgerContent> mlist2) {
    if (null == mlist) {
      mlist = new ArrayList<>();
    }
    PriceListAdapter mAdapter = new PriceListAdapter(getActivity(), mlist2);
    setListAdapter(mAdapter);
    ((PriceListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {

    volleyPost();

    super.onRefresh(refreshView);
  }
}
